/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 14.03.12
 * Time: 14:33
 * To change this template use File | Settings | File Templates.
 */
function Mode2(x, y, w, h, text, type, fill) {
    this.x = x || 0;
    this.y = y || 0;
    this.w = w || 1;
    this.h = h || 1;
    this.fill = fill || '#AAAAAA';
    this.text = text || "Default";
    this.type = type || GameModeSelection.PLAYERvsPLAYER;
    this.border = false;

}

Mode2.method('draw',function () {
    ctx.fillStyle = this.fill;
    ctx.fillRect(this.x, this.y, this.w, this.h);
    ctx.fillStyle = "#000000";
    ctx.font="30px Arial";
    ctx.fillText(this.text, this.x + 10, this.y + (this.h /2) + 10);
    if (this.border){
        ctx.strokeStyle = "#FF0000";
        ctx.strokeRect(this.x-1, this.y-1, this.w+1, this.h+1);

    }
});
Mode2.method('setBorder', function(border){
    this.border = border;
});
Mode2.method('isWithinBounds',function (mx, my) {
    return  (this.x <= mx) && (this.x + this.w >= mx) &&
        (this.y <= my) && (this.y + this.h >= my);
});

function GameModes2(){
    canvas.addEventListener('mousedown', mouseDown, false);
    canvas.addEventListener('mousemove', mouseMove, false);
    this.gameModeList = new Array();
}
GameModes2.inherits(Mode2);
GameModes2.method('addMode',function (mode) {
    this.gameModeList.push(mode);
});
GameModes2.method('getGameModeList', function(){
    return this.gameModeList;
});
GameModes2.method('draw',function () {
    for (var i = 0; i < this.gameModeList.length; i++) {
        this.gameModeList[i].draw();
    }
});

function mouseDown(e) {
    var gameModeList = gameModes.getGameModeList();
    var mouse = getMouse(e);
    var mx = mouse.x;
    var my = mouse.y;
    //alert(gameModeList.length);
    var l = gameModeList.length;
    for (var i = l - 1; i >= 0; i--) {
        if (gameModeList[i].isWithinBounds(mx, my)) {
            gameModeSelected = true;
            switch (gameModeList[i].type){
                case GameModeSelection.PLAYERvsPLAYER:
                    initTwoPlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                case GameModeSelection.PLAYERvsAI:
                    initOnePlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                case GameModeSelection.MultiPLAYER:
                    initTwoPlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                default:
                    break;
            }
        }
    }
}

function  mouseMove(e) {
    var gameModeList = gameModes.getGameModeList();
    var mouse = getMouse(e);
    var mx = mouse.x;
    var my = mouse.y
    var l = gameModeList.length;
    for (var i = l - 1; i >= 0; i--) {
        if (gameModeList[i].isWithinBounds(mx, my)) {
            gameModeList[i].setBorder(true);

        }else{
            gameModeList[i].setBorder(false);
        }
    }

}